package com.uusee.crawler.pageprocessor.video.ku6;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.uusee.crawler.framework.Processor;
import com.uusee.crawler.model.CrawlStatusCodes;
import com.uusee.crawler.model.CrawlURI;
import com.uusee.crawler.util.PageProcessorUtils;
import com.uusee.shipshape.gt.model.UploadUser;

public class Ku6GetUserInterfacePageProcessor extends Processor {
	private boolean canProcess(CrawlURI crawlURI) {
		int crawlStatus = crawlURI.getCrawlStatus();
		if (crawlStatus == CrawlStatusCodes.FETCH_SUCCESS) {
			return true;
		} else if (crawlStatus == CrawlStatusCodes.FETCH_SC_NOT_OK) {
			crawlURI.setCrawlStatus(CrawlStatusCodes.PAGE_PROCESS_INVALID);
			return false;
		} else {
			return false;
		}
	}
	
	//{"data":{"count":0,"list":[{"desc":"","id":1796632,"nick":"\u62cd\u62cd\u62cd\u5ba2","url":"tvet","icon":"http\u003a//image\u002eku6\u002ecom/face/v/24/202/1796632/photo_5tg6_2\u002ejpg"}],"page":1,"size":0},"status":1}
	public void innerProcess(CrawlURI crawlURI) {
		if (!canProcess(crawlURI)) {
			return;
		}
		List<CrawlURI> uploadUserCrawlURIList = new ArrayList<CrawlURI>();
		
		Date crawlDate = new Date();
		String crawlUser = "uusee-crawler";
		String videoSite = crawlURI.getSourceSite();
		String crawlUrl = crawlURI.getCrawlUrl();
		String crawlResult = crawlURI.getCrawlResult();
		
		try {
			JSONObject jsonObject = JSONObject.fromObject( crawlResult ); 
			JSONArray items = new JSONArray();
			if (jsonObject.containsKey("data")) {
				JSONObject dataObject = jsonObject.getJSONObject("data");
				items = dataObject.getJSONArray("list");
			}
			if (items.size()==0) {
				crawlURI.setCrawlStatus(CrawlStatusCodes.PAGE_PROCESS_INVALID);
				return;
			}
			
			Iterator it = items.iterator();
			while(it.hasNext()){
				JSONObject item = (JSONObject)it.next();
				String userId = item.getString("id");
				String username = PageProcessorUtils.unicodeToChinese(item.getString("url"));
				String nick = PageProcessorUtils.unicodeToChinese(item.getString("nick"));
				String logo = PageProcessorUtils.unicodeToChinese(item.getString("icon"));
				
				UploadUser uploadUser = new UploadUser();
				uploadUser.setSourceSite("www.ku6.com");
				uploadUser.setUserId(userId);
				uploadUser.setUsername(username);
				uploadUser.setLogo(logo);
				uploadUser.setUserBlogUrl("http://v.ku6.com/"+username);
				uploadUser.setUpdateUser(crawlUser);
				uploadUser.setUpdateDate(crawlDate);

				
				CrawlURI uploadUserCrawlURI = new CrawlURI();
				uploadUserCrawlURI.setSourceSite(videoSite);
				uploadUserCrawlURI.setCrawlUrl(uploadUser.getUserBlogUrl());
				uploadUserCrawlURI.setCharset("utf-8");
				uploadUserCrawlURI.setCrawlStatus(CrawlStatusCodes.PAGE_PROCESS_SUCCESS);
				uploadUserCrawlURI.setModel(uploadUser);
				
				uploadUserCrawlURIList.add(uploadUserCrawlURI);
			}
			
			if (uploadUserCrawlURIList.size()>0){
				crawlURI.setModel(uploadUserCrawlURIList);
				crawlURI.setCrawlStatus(CrawlStatusCodes.PAGE_PROCESS_SUCCESS);
			}else{
				crawlURI.setCrawlStatus(CrawlStatusCodes.PAGE_PROCESS_INVALID);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			crawlURI.setCrawlStatus(CrawlStatusCodes.PAGE_PROCESS_EXCEPTION);
			throw new RuntimeException("处理KU6取得用户接口页失败。");
		}
	}
}
